package lomasky.xui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

public class ItemDivider extends RecyclerView.ItemDecoration {
    private Drawable mDivider;
    private  int paddindLeftAndRight;

    public ItemDivider(Context context) {
        mDivider= ContextCompat.getDrawable(context, R.drawable.layout_divider1px);

    }

    /**
     *
     * @param context
     * @param paddindLeftAndRight 单位dp
     */
    public ItemDivider(Context context,int paddindLeftAndRight) {
        mDivider= ContextCompat.getDrawable(context, R.drawable.layout_divider1px);

        this.paddindLeftAndRight= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddindLeftAndRight, context.getResources().getDisplayMetrics());;
    }



    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft()+paddindLeftAndRight;
        int right = parent.getWidth() - parent.getPaddingRight()-paddindLeftAndRight;

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}